﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.DAO
{
    public class ElectricDevice_DAO
    {
        private static QL_DoanhNghiepDataContext data = new QL_DoanhNghiepDataContext();

        public static void CatalogueIDOfElectricDevice(string ElectricDeviceID, ref int ParentCatalogueID, ref int ChildCatalogueID)
        {
            var list = from ed in data.ELECTRIC_DEVICEs
                       join cc in data.CHILD_CATALOGUEs on ed.ChildCatalogue_ID equals cc.ChildCatalogue_ID
                       join pc in data.PARENT_CATALOGUEs on cc.ParentCatalogue_ID equals pc.ParentCatalogue_ID
                       where ed.ElectricDevice_ID == ElectricDeviceID && ed.Status == 1
                       select new
                       {
                           cc.ChildCatalogue_ID,
                           pc.ParentCatalogue_ID,
                       };
            foreach (var item in list)
            {
                ParentCatalogueID = item.ParentCatalogue_ID;
                ChildCatalogueID = item.ChildCatalogue_ID;
            }
        }

        public static List<ElectricDevice_DTO> DetailElectricDevice(string ElectricDeviceID)
        {
            List<ElectricDevice_DTO> listElectricDevice = new List<ElectricDevice_DTO>();
            var list = from ed in data.ELECTRIC_DEVICEs
                       join b in data.BRANDs on ed.Brand_ID equals b.Brand_ID
                       join c in data.CURRENCies on ed.Currency_ID equals c.Currency_ID
                       where ed.ElectricDevice_ID == ElectricDeviceID && ed.Status == 1
                       select new
                       {
                           ed.ElectricDevice_ID,
                           ed.ElectricDevice_Name,
                           ed.RetailPrice,
                           ed.DealerPrice,
                           ed.Size,
                           ed.Capacity,
                           ed.Quanlity,
                           ed.Quantity,
                           ed.NumberOfBuyers,
                           ed.Description,
                           ed.ImagePath,
                           ed.InsertedDate,
                           ed.ModifiedDate,
                           c.Currency_ID,
                           c.Currency_Name,
                           b.Brand_ID,
                           b.Brand_Name,
                       };
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.RetailPrice = float.Parse(item.RetailPrice).ToString("###,###,###");
                ed.DealerPrice = float.Parse(item.DealerPrice).ToString("###,###,###");
                ed.Size = item.Size;
                ed.Capacity = item.Capacity;
                ed.Quanlity = item.Quanlity;
                ed.Quantity = (int)item.Quantity;
                if (item.NumberOfBuyers != null)
                    ed.NumberOfBuyers = (int)item.NumberOfBuyers;
                ed.Description = item.Description;
                ed.ImagePath = item.ImagePath;
                ed.InsertedDate = (DateTime)item.InsertedDate;
                if (item.ModifiedDate != null)
                    ed.ModifiedDate = (DateTime)item.ModifiedDate;
                ed.Brand_Name = item.Brand_Name;
                ed.Brand_ID = item.Brand_ID;
                ed.Currency_ID = item.Currency_ID;
                ed.Currency_Name = item.Currency_Name;
                listElectricDevice.Add(ed);
            }
            return listElectricDevice;
        }

        public static List<ElectricDevice_DTO> DetailElectricDeviceByCouponID(string CouponID)
        {
            List<ElectricDevice_DTO> listElectricDevice = new List<ElectricDevice_DTO>();
            var list = from ed in data.ELECTRIC_DEVICEs
                       join cp in data.COUPONs on ed.ElectricDevice_ID equals cp.ElectricDevice_ID
                       join b in data.BRANDs on ed.Brand_ID equals b.Brand_ID
                       join c in data.CURRENCies on ed.Currency_ID equals c.Currency_ID
                       where ed.Status == 1 && cp.Coupon_ID == CouponID
                       select new
                       {
                           ed.ElectricDevice_ID,
                           ed.ElectricDevice_Name,
                           ed.RetailPrice,
                           ed.DealerPrice,
                           ed.Size,
                           ed.Capacity,
                           ed.Quanlity,
                           ed.Quantity,
                           ed.NumberOfBuyers,
                           ed.Description,
                           ed.ImagePath,
                           ed.InsertedDate,
                           ed.ModifiedDate,
                           c.Currency_ID,
                           c.Currency_Name,
                           b.Brand_ID,
                           b.Brand_Name,
                       };
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.RetailPrice = float.Parse(item.RetailPrice).ToString("###,###,###");
                ed.DealerPrice = float.Parse(item.DealerPrice).ToString("###,###,###");
                ed.Size = item.Size;
                ed.Capacity = item.Capacity;
                ed.Quanlity = item.Quanlity;
                ed.Quantity = (int)item.Quantity;
                if (item.NumberOfBuyers != null)
                    ed.NumberOfBuyers = (int)item.NumberOfBuyers;
                ed.Description = item.Description;
                ed.ImagePath = item.ImagePath;
                ed.InsertedDate = (DateTime)item.InsertedDate;
                if (item.ModifiedDate != null)
                    ed.ModifiedDate = (DateTime)item.ModifiedDate;
                ed.Brand_Name = item.Brand_Name;
                ed.Brand_ID = item.Brand_ID;
                ed.Currency_ID = item.Currency_ID;
                ed.Currency_Name = item.Currency_Name;
                listElectricDevice.Add(ed);
            }
            return listElectricDevice;
        }

        public static List<ElectricDevice_DTO> ListProductByParentCatalogue(int ParentCatalogueID)
        {
            List<ElectricDevice_DTO> listproduct = new List<ElectricDevice_DTO>();
            var list = from ed in data.ELECTRIC_DEVICEs
                       join cc in data.CHILD_CATALOGUEs on ed.ChildCatalogue_ID equals cc.ChildCatalogue_ID
                       join pc in data.PARENT_CATALOGUEs on cc.ParentCatalogue_ID equals pc.ParentCatalogue_ID
                       join c in data.CURRENCies on ed.Currency_ID equals c.Currency_ID
                       where pc.ParentCatalogue_ID == ParentCatalogueID && ed.Status == 1
                       select new
                       {
                           ed.ElectricDevice_ID,
                           ed.ElectricDevice_Name,
                           ed.ImagePath,
                           ed.Description,
                           ed.Quanlity,
                           ed.Quantity,
                           ed.RetailPrice,
                           ed.DealerPrice,
                           ed.Size,
                           ed.Status,
                           ed.Currency_ID,
                           ed.Brand_ID,
                       };
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ImagePath = item.ImagePath;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.Description = item.Description;
                ed.Quantity = (int)item.Quantity;
                ed.RetailPrice = float.Parse(item.RetailPrice).ToString("###,###,###");
                ed.DealerPrice = float.Parse(item.DealerPrice).ToString("###,###,###");
                ed.Currency_ID = item.Currency_ID;
                ed.Brand_ID = (int)item.Brand_ID;
                listproduct.Add(ed);
            }
            return listproduct;
        }

        public static List<ElectricDevice_DTO> ListAllProduct()
        {
            List<ElectricDevice_DTO> listproduct = new List<ElectricDevice_DTO>();
            var list = from ed in data.ELECTRIC_DEVICEs
                       select new
                       {
                           ed.ElectricDevice_ID,
                           ed.ElectricDevice_Name,
                           ed.ImagePath,
                           ed.Description,
                           ed.Quanlity,
                           ed.Quantity,
                           ed.RetailPrice,
                           ed.DealerPrice,
                           ed.Size,
                           ed.Status,
                           ed.Currency_ID,
                           ed.Brand_ID,
                       };
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ImagePath = item.ImagePath;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.Description = item.Description;
                ed.Quantity = (int)item.Quantity;
                ed.RetailPrice = float.Parse(item.RetailPrice).ToString("###,###,###");
                ed.DealerPrice = float.Parse(item.DealerPrice).ToString("###,###,###");
                ed.Currency_ID = item.Currency_ID;
                ed.Brand_ID = (int)item.Brand_ID;
                listproduct.Add(ed);
            }
            return listproduct;
        }

        public static List<ElectricDevice_DTO> FastSearch(string keyword)
        {
            List<ElectricDevice_DTO> listproduct = new List<ElectricDevice_DTO>();
            var list = data.spFastSearch(keyword);
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ImagePath = item.ImagePath;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.Description = item.Description;
                ed.Quantity = (int)item.Quantity;
                ed.RetailPrice = item.RetailPrice;
                ed.DealerPrice = item.DealerPrice;
                ed.Currency_ID = item.Currency_ID;
                ed.Brand_ID = (int)item.Brand_ID;
                listproduct.Add(ed);
            }
            return listproduct;
        }

        public static List<ElectricDevice_DTO> ListNewProduct()
        {
            List<ElectricDevice_DTO> listproduct = new List<ElectricDevice_DTO>();
            var list = (from ed in data.ELECTRIC_DEVICEs
                        join cc in data.CHILD_CATALOGUEs on ed.ChildCatalogue_ID equals cc.ChildCatalogue_ID
                        join pc in data.PARENT_CATALOGUEs on cc.ParentCatalogue_ID equals pc.ParentCatalogue_ID
                        join c in data.CURRENCies on ed.Currency_ID equals c.Currency_ID
                        where ed.Status == 1
                        orderby ed.InsertedDate descending
                        select new
                        {
                            ed.ElectricDevice_ID,
                            ed.ElectricDevice_Name,
                            ed.ImagePath,
                            ed.Description,
                            ed.Quanlity,
                            ed.Quantity,
                            ed.RetailPrice,
                            ed.DealerPrice,
                            ed.Size,
                            ed.Status,
                            c.Currency_ID,
                        }).Skip(0).Take(10);
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ImagePath = item.ImagePath;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.Description = item.Description;
                ed.Quantity = (int)item.Quantity;
                ed.RetailPrice = float.Parse(item.RetailPrice).ToString("###,###,###");
                ed.DealerPrice = float.Parse(item.DealerPrice).ToString("###,###,###");
                ed.Currency_ID = item.Currency_ID;
                listproduct.Add(ed);
            }
            return listproduct;
        }

        public static List<ElectricDevice_DTO> ListUpdateProduct()
        {
            List<ElectricDevice_DTO> listproduct = new List<ElectricDevice_DTO>();
            var list = (from ed in data.ELECTRIC_DEVICEs
                        join cc in data.CHILD_CATALOGUEs on ed.ChildCatalogue_ID equals cc.ChildCatalogue_ID
                        join pc in data.PARENT_CATALOGUEs on cc.ParentCatalogue_ID equals pc.ParentCatalogue_ID
                        join c in data.CURRENCies on ed.Currency_ID equals c.Currency_ID
                        where ed.Status == 1
                        orderby ed.ModifiedDate descending
                        select new 
                        {
                            ed.ElectricDevice_ID,
                            ed.ElectricDevice_Name,
                            ed.ImagePath,
                            ed.Description,
                            ed.Quanlity,
                            ed.Quantity,
                            ed.RetailPrice,
                            ed.DealerPrice,
                            ed.Size,
                            ed.Status,
                            c.Currency_ID,
                        }).Skip(0).Take(10);
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ImagePath = item.ImagePath;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.Description = item.Description;
                ed.Quantity = (int)item.Quantity;
                ed.RetailPrice = float.Parse(item.RetailPrice).ToString("###,###,###");
                ed.DealerPrice = float.Parse(item.DealerPrice).ToString("###,###,###");
                ed.Currency_ID = item.Currency_ID;
                listproduct.Add(ed);
            }
            return listproduct;
        }

        public static List<ElectricDevice_DTO> ListProductByChildCatalogue(int ChildCatalogueID)
        {
            List<ElectricDevice_DTO> listproduct = new List<ElectricDevice_DTO>();
            var list = from ed in data.ELECTRIC_DEVICEs
                       join cc in data.CHILD_CATALOGUEs on ed.ChildCatalogue_ID equals cc.ChildCatalogue_ID
                       join pc in data.PARENT_CATALOGUEs on cc.ParentCatalogue_ID equals pc.ParentCatalogue_ID
                       join c in data.CURRENCies on ed.Currency_ID equals c.Currency_ID
                       where cc.ChildCatalogue_ID == ChildCatalogueID && ed.Status == 1
                       select new
                       {
                           ed.ElectricDevice_ID,
                           ed.ElectricDevice_Name,
                           ed.ImagePath,
                           ed.Description,
                           ed.Quanlity,
                           ed.Quantity,
                           ed.RetailPrice,
                           ed.DealerPrice,
                           ed.Size,
                           ed.Status,
                           c.Currency_ID,
                       };
            foreach (var item in list)
            {
                ElectricDevice_DTO ed = new ElectricDevice_DTO();
                ed.ElectricDevice_ID = item.ElectricDevice_ID;
                ed.ImagePath = item.ImagePath;
                ed.ElectricDevice_Name = item.ElectricDevice_Name;
                ed.Description = item.Description;
                ed.Quantity = (int)item.Quantity;
                ed.RetailPrice = float.Parse(item.RetailPrice).ToString("###,###,###");
                ed.DealerPrice = float.Parse(item.DealerPrice).ToString("###,###,###");
                ed.Currency_ID = item.Currency_ID;
                listproduct.Add(ed);
            }
            return listproduct;
        }

        public static bool CheckNameElectricDevice(string ElectricDeviceName)
        {
            var list = from e in data.ELECTRIC_DEVICEs
                       where e.ElectricDevice_ID == ElectricDeviceName
                       select new
                       {
                           e.ElectricDevice_Name,
                       };
            if (list.Count() > 0)
                return false;
            return true;
        }

        public static void DeleteElectricDevice(string ElectricDevice_ID)
        {
            data.spDeleteElectricDevice(ElectricDevice_ID);
        }

        public static void AddProduct(ELECTRIC_DEVICE electricdevice)
        {
            try
            {
                data.ELECTRIC_DEVICEs.InsertOnSubmit(electricdevice);
                data.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void UpdateElectricDevice(ELECTRIC_DEVICE electricdevice)
        {
            data.spUpdateElectricDevice(electricdevice.ElectricDevice_ID, electricdevice.RetailPrice, electricdevice.DealerPrice, electricdevice.Quantity, electricdevice.Quanlity, electricdevice.Size, electricdevice.Capacity, electricdevice.Description, electricdevice.ModifiedDate, electricdevice.ImagePath, electricdevice.Brand_ID, electricdevice.ChildCatalogue_ID, electricdevice.Currency_ID);
        }

        public static List<CURRENCY> GetCurrencyOfProduct()
        {
            List<CURRENCY> listcurrency = new List<CURRENCY>();
            var list = from c in data.CURRENCies
                       select c;
            foreach (var item in list)
            {
                CURRENCY c = new CURRENCY();
                c.Currency_ID = item.Currency_ID;
                c.Currency_Name = item.Currency_Name;
                listcurrency.Add(c);
            }
            return listcurrency;
        }
    }
}
